package com.jerry.template.module.mainpage.shouye.jinghua.fragment;

import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.base.BaseFragment;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mainpage.shouye.jinghua.adapter.jinghuaListViewAdapter;
import com.jerry.template.module.mainpage.shouye.jinghua.entity.Jhentity;
import com.jerry.template.module.mainpage.shouye.jinghua.entity.JinghuaEntity;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 16-5-13.
 */
public class EssenceFragment extends BaseFragment{
    //界面上面显示的一个图片
    @BindView(R.id.jinghua_newestPic)
    public ImageView newestPic;
    //界面下面的一个listView
    @BindView(R.id.jinghua_detail_list)
    public PullToRefreshListView jinghuaListView;
    //集合
    private List<Jhentity.DataBean>list;
    //listView 适配器
    private jinghuaListViewAdapter adapter;
    //图片加载方式
    private ImageLoader imageLoader;
    private DisplayImageOptions options;

    //这个fragment要加载的界面
    @Override
    protected int getLayoutId() {

        return R.layout.fragment_jinghua;

    }
    //控件初始化
    @Override
    protected void initViews() {
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
        //下拉刷新设置
        jinghuaListView.getRefreshableView().setDivider(null);
        jinghuaListView.getRefreshableView().setSelector(android.R.color.transparent);
        //设置刷新模式
        jinghuaListView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        //设置监听器
        jinghuaListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新的监听
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
            //上拉刷新的监听
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //加载下一页数据
                count+=5;
                list.clear();
                loadData();

            }
        });
        list = new ArrayList<>();
        adapter=new jinghuaListViewAdapter(getContext(),list);
        jinghuaListView.setAdapter(adapter);
    }

    //精华页面加载item的个数
    private int count = 10;

    //加载数据
    @Override
    protected void loadData() {
        //访问网络接口
        Map<String,Object>params = new HashMap<>();
        params.put("","");
        OkHttpUtils.get(Urls.JINGHUA_URL_BEFORE
                +count
                +Urls.JINGHUA_URL_AFTER,params);
    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if(message!=null){
            if(message.type.equals(Urls.JINGHUA_URL_BEFORE
                    +count
                    +Urls.JINGHUA_URL_AFTER)){

                String result = message.result;
                Log.e("postEventResult",result);
                Gson gson = new Gson();
                Jhentity entity = gson.fromJson(result,Jhentity.class);

                //精华顶部图片

                imageLoader.displayImage(entity.getData().get(0).getImage_url(),newestPic,options);
                //获取的数据加入集合
                list.addAll(entity.getData());

                //数据更新
                adapter.notifyDataSetChanged();
                jinghuaListView.onRefreshComplete();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        list.clear();

    }
}
